package com.changxianggu.crawler.cloud.test;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.changxianggu.cloud.crawler.core.WebApplication;
import com.changxianggu.cloud.crawler.core.modules.storage.entity.CxgBook;
import com.changxianggu.cloud.crawler.core.modules.storage.service.ICxgBookService;
import com.changxianggu.cloud.crawler.core.utils.HttpUtil;
import com.changxianggu.cloud.crawler.core.utils.UserAgentUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {WebApplication.class})// 指定启动类
@Slf4j
public class LinYeXiaoTu {

    @Autowired
    ICxgBookService bookService;

    public static final String COOKIE = "_session=eyJpdiI6Im5MNjVUbUY4ZW9xdFdxdUUzK0VRR2c9PSIsInZhbHVlIjoiNzJmbVZzK1VMM0Fia3JHZ1dkRXpINzZ6bmJpV0p6cEVZQ3o4NllJWE9BcFBmVHI1VTUrd0o5eXVOSGI0NEFETyIsIm1hYyI6IjZkNjlkMjU5MjEwYTVhZjNmNmNlNTAxNDZlODhlYTFlNGQyNTJiODU2NWZmMTVmMGQwNDk4NDA3MmRmNDhkNjQifQ%3D%3D";
    public static final String REFERER = "https://edu.cfph.net/ebook/category?keywords=9787&page=1&ebook_subject_id=&ebook_type_id=1&price_sale=";
    public static final String URL_PRE = "https://edu.cfph.net/ebook/index/preview?ebook_id=";


    @Test
    public void initCrawler() throws Exception {
        doCrawler();
    }

    public void doCrawler() throws Exception {
        int pageNum = 1;
        int totalPage = 6;
        int pageSize = 15;
        for (int i = pageNum; i <= totalPage; i++) {
            String url = "https://edu.cfph.net/api/public/ebook/list";
            Map<String, Object> parameter = MapUtil.newHashMap(7);
            parameter.put("page", i);
            parameter.put("ebook_type_id", "1");
            parameter.put("size", pageSize);
            Map<String, Object> headers = MapUtil.newHashMap(5);
            headers.put("Cookie", COOKIE);
            headers.put("User-Agent", UserAgentUtil.random());
            headers.put("Referer", REFERER);
            String result = HttpUtil.doGet(url, headers, parameter);
            if (StrUtil.isNotBlank(result)) {
                JSONObject jsonObject = JSONObject.parseObject(result);
                Integer errcode = jsonObject.getInteger("errno");
                if (errcode != null && errcode == 0) {
                    JSONObject dataJsonObject = jsonObject.getJSONObject("data");
                    JSONArray listObject = dataJsonObject.getJSONArray("list");
                    int size = listObject.size();
                    List<CxgBook> listBook = new ArrayList<>();
                    for (int k = 0; k < size; k++) {
                        JSONObject jsonTmp = listObject.getJSONObject(k);
                        CxgBook bookEntity = new CxgBook();
                        bookEntity.setBookName(jsonTmp.getString("ebook_name"));
                        bookEntity.setBookIsbn(jsonTmp.getString("ebook_isbn"));
                        //bookEntity.setCover(jsonTmp.getString("picture"));
                        bookEntity.setBrief(jsonTmp.getString("introduction"));
                        bookEntity.setPrice(BigDecimal.valueOf(jsonTmp.getDouble("ebook_price")));
                        bookEntity.setAuthor(jsonTmp.getString("author_name"));
                        String created_at = jsonTmp.getString("created_at");
                        if (StrUtil.isNotBlank(created_at)) {
                            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                            LocalDateTime localDateTime = LocalDateTime.parse(created_at, dtf);
                            bookEntity.setPubDate(localDateTime);
                        }
                        bookEntity.setIsSync(0);
                        bookEntity.setCreateTime(LocalDateTime.now());
                        bookEntity.setPressName("中国林业出版社");
                        bookEntity.setStatus(1);
                        bookEntity.setPressId(163);
                        String tmpString = jsonTmp.getString("id");
                        bookEntity.setPkId(tmpString);
                        bookEntity.setDigitalUri(URL_PRE+tmpString);
                        listBook.add(bookEntity);
                    }
                    bookService.saveBatch(listBook);
                    log.info("第" + i + "页数据保存成功");
                    log.warn("--->" + i);
                }
            }
        }
    }

    /**
     * 判断一个字符串是否都为数字
     *
     * @param strNum
     * @return
     */
    public boolean isDigit(String strNum) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) strNum);
        return matcher.matches();
    }

    /**
     * 截取数字
     *
     * @param content
     * @return
     */
    public String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
}
